package core.remote;

import core.GlobalConfig;
import core.IncomeMessage;
import core.Main;
import core.IMessageSender;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import tools.Logger;
import tools.SecLib;

/**
 * Init client Remote console
 * @author dcrm
 */

class ClientRC extends Thread implements IMessageSender {

    private CoreRC server = null;
    private Socket socket = null;
    private int ID = -1;
    private DataInputStream streamIn = null;
    private DataOutputStream streamOut = null;
    private boolean adm = false;

    public ClientRC(CoreRC _server, Socket _socket) {
        super();
        try {
            server = _server;
            socket = _socket;
            ID = socket.getPort();
        } catch (Exception ex) {Logger.writelnErr(ID +"Error ; "+ex.getMessage());}
    }

    public void send(String msg) {
        try {
            if (GlobalConfig.F_SECURITY_KEY) {msg=SecLib.encodeBlowfishUTF8Text(msg, GlobalConfig.getKey());}
            streamOut.writeUTF(msg);
            streamOut.flush();
        }
        catch (Exception ex) {Logger.writelnErr(ID +"Error sending; "+ex.getMessage()); server.remove(ID); stop();}
    }

    public int getID() { return ID; }
    public boolean isADM() { return adm; }
    public void isADM(boolean set) { adm=set; }

    @Override
    public void run() {
        try {
        Main.pr("\nServer Thread "+ID+" - running.\n");
        while (true) {
            try {
                String incmsg=streamIn.readUTF();
                if (!incmsg.isEmpty()) {
                    if (GlobalConfig.F_SECURITY_KEY) {incmsg=SecLib.decodeBlowfishUTF8Text(incmsg, GlobalConfig.getKey());}
                    if (incmsg.startsWith(".") || incmsg.startsWith("#")) {
                        server.handle(ID, incmsg);
                    }
                    else if (incmsg.startsWith("!")) {
                        incmsg=incmsg.substring(1);
                        new IncomeMessage(incmsg, Integer.toString(ID), ((isADM())?Integer.toString(ID):null), 5000, "ChatC["+Integer.toString(ID)+"]", this);
                    } else {server.handle(ID, incmsg);}
                }
            } catch (IOException ex) {Logger.writelnErr(ID +"Error reading; "+ex.getMessage()); server.remove(ID); stop();} }
        } catch (Exception ex0) {Logger.writelnErr(ex0.getMessage());}
    }

    public void open() {
        try {
            streamIn = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
            streamOut = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
        } catch (Exception ex) {Logger.writelnErr(ex.getMessage());}
    }
    
    public void close() {
        try {
            if (socket !=null) socket.close();
            if (streamIn !=null) streamIn.close();
            if (streamOut != null) streamOut.close();
        } catch (Exception ex) {Logger.writelnErr(ex.getMessage()); }
    }

    public void sendBAdmReport(boolean b, String s) {
        try { if (isADM()) send(s + " - state: " + ((b) ? "ON" : "OFF"));
        } catch (Exception ex) {Logger.writelnErr(ex.getMessage());}
    }

    public void sendAdMsg(String msg) {
        try { if (isADM()) send(msg);
        } catch (Exception ex) {Logger.writelnErr(ex.getMessage());}
    }

    public void sendAdEMsg(String msg) {
        sendAdMsg(msg);
    }

    public void sendMessage(String to, String msg) {
        try { send(msg);
        } catch (Exception ex) {Logger.writelnErr(ex.getMessage());}
    }

    public void sendExtendedMessage(String to, String msg) {
        sendMessage(to, msg);
    }

    public void reConnect() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void disconnect(boolean bl) {
        try { server.stop();
        } catch (Exception ex) {Logger.writelnErr(ex.getMessage());}
    }

    public String getInfo() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
